package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.media.tv.TvContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.tools.CalculateTimeDifference;
import com.example.dllo.keepdemo.tools.RoundImageView;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.bean.TrainDynamicBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by dllo on 16/1/15.
 */
public class TrainWorkoutsListViewAdapter extends BaseAdapter {
    private Context context;
    private TrainDynamicBean data;

    public TrainWorkoutsListViewAdapter(Context context,TrainDynamicBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_activity_trainworkouts,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        if (data.getData().get(position).getContent().equals("")) {
            myViewHolder.tvContent.setVisibility(View.GONE);
        }
        myViewHolder.tvContent.setText(data.getData().get(position).getContent());

        myViewHolder.tvUserName.setText(data.getData().get(position).getAuthor().getUsername());
        myViewHolder.tvFinishtime.setText(CalculateTimeDifference.timeDifferenceUTC(data.getData().get(position).getCreated()));

        String url = data.getData().get(position).getAuthor().getAvatar();
        if ((url.equals("")) || !(url.endsWith(".jpg"))) {
            myViewHolder.imgHead.setImageResource(R.mipmap.person_45_45);
        } else {
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imgHead,R.mipmap.person_45_45,R.mipmap.ic);
            VolleySingleton.getImageLoader().get(url,listener);
        }

        return convertView;
    }

    class MyViewHolder {
        private TextView tvContent,tvUserName,tvFinishtime;
        private RoundImageView imgHead;
        public MyViewHolder(View view) {
            tvContent = (TextView) view.findViewById(R.id.tv_item_activity_trainworkouts_content);
            tvUserName = (TextView) view.findViewById(R.id.tv_item_activity_trainworkouts_username);
            tvFinishtime = (TextView) view.findViewById(R.id.tv_item_activity_trainworkouts_finishtime);
            imgHead = (RoundImageView) view.findViewById(R.id.roundimg_item_activity_trainworkouts);
        }
    }
}
